package cn.ecut.controller;


import cn.ecut.model.*;
import cn.ecut.service.FangjianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.*;

@Controller
@RequestMapping("/fangjian")
public class FangjianController {
    @Autowired
    private FangjianService fangjianService;

    @ResponseBody
    @RequestMapping("/show")
    public HashMap show() {
        HashMap<String, Object> map = new HashMap<String, Object> ();
        Integer count = fangjianService.selectAllcount();
        List<Fangjian> list = new ArrayList<>();
        list=fangjianService.selectAll();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        return map;
    }

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Fangjian fangjian,String loudongbianma){
        System.out.println(loudongbianma);
        Message message = new Message();
        Loudong loudong = new Loudong();
        Taofang taofang = new Taofang();
        loudong = fangjianService.selectLoudong(loudongbianma);
        taofang = fangjianService.selectTaofang(fangjian.getTaofangbianma());
        fangjian.setQuyu(taofang.getSuoshuquyu());
        fangjian.setLoudong(loudong.getLoudong());
        fangjian.setTaofang(taofang.getTaofangmingcheng());
        fangjian.setFangjianbianma(fangjianService.getfangjianBianma());
        fangjian.setShifouyoupuwei(1);
        fangjian.setShiyongzhuangtai(1);
        int n = fangjianService.insert(fangjian);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/update")
    public Message update(Fangjian fangjian){
        Message message = new Message();
        int n = fangjianService.updateByPrimaryKey(fangjian);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Fangjian fangjian){
        Message message = new Message();
        if(fangjian.getQiyongzhuangtai()==0){
            fangjian.setQiyongzhuangtai(1);
        }else {
            fangjian.setQiyongzhuangtai(0);
        }
        int n = fangjianService.updateQiyong(fangjian);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String fangjianbianma) {
        Message message = new Message();
        int n = fangjianService.deleteByPrimaryKey(fangjianbianma);
        if (n == 1) {
            message.setFlag(true);
            return message;
        } else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap select(String searchResult){
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Fangjian> list = new ArrayList<>();
        list = fangjianService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }
}
